// index.js
// 获取应用实例
var util = require('../../utils/md5.js')
const app = getApp()
Page({
  data: {
    safeTop: '',
    mobile: '',
    srtcheck: true, // 隐私协议
    hiddenSrtInfo: true
  },
  onLoad() {
    this.setData({
      safeTop: app.globalData.safeTop
    })
  },
  // 点击隐私协议框
  checkboxChange(e) {
    let that = this
    that.setData({
      srtcheck: !that.data.srtcheck
    })
  },

  // 点击隐私协议 显示详情
  showSrtInfo() {
    let that = this
    that.setData({
      hiddenSrtInfo: false
    })
  },

  // 同意
  Agree() {
    let that = this
    that.setData({
      srtcheck: true,
      hiddenSrtInfo: true
    })
  },
  // 进入
  toNext() {
    let that = this
    let mobile = that.data.mobile
    if (mobile == '') {
      return wx.showToast({
        title: '请输入您的手机号',
        icon: 'none',
      })
    }
    if (!that.data.srtcheck) {
      return wx.showToast({
        title: '请同意并阅读隐私政策',
        icon: 'none',
      })
    }
    that.getDataByPhoneNumber(mobile)
  },
  getDataByPhoneNumber(mobile) {
    wx.showLoading({
      title: 'loading',
      icon: 'loading'
    })
    let timestamp = parseInt(new Date().getTime() / 1000)
    let smsg = util.hexMD5(`ek8yaqavc9uhs3ybxmycd_${timestamp}`).toLowerCase()
    let token = smsg.substring(5, 15)
    wx.request({
      method: 'POST',
      url: 'https://gac.mahelei.com/guinness/index/getVideoAndImagesByMobile',
      data: {
        mobile
      },
      header: {
        'content-type': 'application/json',
        timestamp,
        token
      },
      success(res) {
        // console.log(res.data)
        if (res.data.code === 200) {
          // 存储数据
          app.globalData.videos = res.data.data.videos
          app.globalData.imgs = res.data.data.canSelectImgs
          app.globalData.fixedImgs = res.data.data.fixedImgs
          app.globalData.isCanPrint = res.data.data.isCanPrint
          app.globalData.uuid = res.data.data.uuid
          app.globalData.qmimg = res.data.data.sign_png_url
          if (res.data.data.canSelectImgs.length > 0) {
            wx.redirectTo({
              url: '/pages/host/host',
            })
          } else {
            wx.redirectTo({
              url: '/pages/waiting/waiting',
            })
          }

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      },
      fail(err) {
        wx.showToast({
          title: err.errMsg,
          icon: 'none'
        })
      },
      complete(e) {
        wx.hideLoading()
      }
    })
  },
})